package com.hrma.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.AchievementMapper;
import com.hrma.entity.Achievement;
import com.hrma.entity.AchievementDTO;
import com.hrma.entity.AttendancePO;
import com.hrma.entity.SelectResult;
import com.hrma.entity.exception.DataIsNull;
import com.hrma.services.AchievementService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class AchievementImpl implements AchievementService{
    private final AchievementMapper achievementMapper;

    public AchievementImpl(AchievementMapper achievementMapper) {
        this.achievementMapper = achievementMapper;
    }

    @Override
    public Achievement add(Achievement data) {
        if (!achievementMapper.exist(data.getId())) {
            throw new RuntimeException("id字段不存在！");
        }
        data.setGrade((data.getSelfGrade() + data.getSuperiorGrade()) / 2);
        data.setBonus(data.getBaseBonus() * data.getGrade() / 100);
        achievementMapper.add(data);
        return data;
    }

    @Override
    public void delete(int id) {
        if (!achievementMapper.exist(id)) {
            throw new RuntimeException("id字段不存在！");
        }
        achievementMapper.delete(id);
    }

    @Override
    public void update(Achievement data) {
        if (!achievementMapper.exist(data.getId())) {
            throw new RuntimeException("id字段不存在！");
        }
        data.setGrade((data.getSelfGrade() + data.getSuperiorGrade()) / 2);
        data.setBonus(data.getBaseBonus() * data.getGrade() / 100);
        achievementMapper.update(data);
    }

    @Override
    public List<SelectResult> select() {
        return achievementMapper.select();
    }

    @Override
    public Page<Achievement> getList(AchievementDTO data) {
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return achievementMapper.getList(data);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, SQLException.class})
    public void handleUpload(MultipartFile multipartFile) throws IOException {
        //上月第一天0点
        Calendar lastMonthFirstDateCal = Calendar.getInstance();
        lastMonthFirstDateCal.add(Calendar.MONTH, -1);
        lastMonthFirstDateCal.set(Calendar.MILLISECOND,0);
        int year = lastMonthFirstDateCal.get(Calendar.YEAR);
        int month = lastMonthFirstDateCal.get(Calendar.MONTH);
        lastMonthFirstDateCal.set(year,month,1,0,0,0);
        Date lastMonthFirstDate = lastMonthFirstDateCal.getTime();
        //本月第一天0点
        Calendar lastMonthEndDateCal = Calendar.getInstance();
        lastMonthEndDateCal.set(Calendar.MILLISECOND,0);
        int year1 = lastMonthEndDateCal.get(Calendar.YEAR);
        int month1 = lastMonthEndDateCal.get(Calendar.MONTH);
        lastMonthEndDateCal.set(year1,month1,1,0,0,0);
        Date lastMonthEndDate = lastMonthEndDateCal.getTime();

        //导入考勤记录excel表
        Workbook wb = new XSSFWorkbook(multipartFile.getInputStream());
        Sheet sheet = wb.getSheetAt(0);
        for (int j = 1; true; j++) {
            Row row = sheet.getRow(j);
            if (row == null) break;
            String num = row.getCell(0).getStringCellValue();
            int baseBonus = (int)row.getCell(1).getNumericCellValue();
            int selfGrade = (int)row.getCell(2).getNumericCellValue();
            String superior = row.getCell(3).getStringCellValue();
            int superiorGrade = (int)row.getCell(4).getNumericCellValue();
            int grade = (selfGrade + superiorGrade) / 2;
            int bonus = baseBonus * grade / 100;

            Achievement achievement = new Achievement();
            achievement.setNum(num);
            achievement.setBaseBonus(baseBonus);
            achievement.setSelfGrade(selfGrade);
            achievement.setSuperior(superior);
            achievement.setSuperiorGrade(superiorGrade);
            achievement.setGrade(grade);
            achievement.setBonus(bonus);
            achievement.setBeginTime(lastMonthFirstDate);
            achievement.setEndTime(lastMonthEndDate);
            achievementMapper.add(achievement);

            if (("".equals(num)) || (num == null) ||
                    (baseBonus == 0) || (selfGrade == 0) || (superior == null) || (superiorGrade == 0)) {
                throw new DataIsNull("第" + (j + 1) + "行数据缺失!");
            }
        }
    }
}
